home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Net / SNPP.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  7.7 KB  |  377 lines

  1.  
  2. package Net::SNPP;
  3.  
  4. require 5.001;
  5.  
  6. use strict;
  7. use vars qw($VERSION @ISA @EXPORT);
  8. use Socket 1.3;
  9. use Carp;
  10. use IO::Socket;
  11. use Net::Cmd;
  12. use Net::Config;
  13.  
  14. $VERSION = do { my @r=(q$Revision: 1.9 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r};
  15. @ISA     = qw(Net::Cmd IO::Socket::INET);
  16. @EXPORT  = qw(CMD_2WAYERROR CMD_2WAYOK CMD_2WAYQUEUED);
  17.  
  18. sub CMD_2WAYERROR  { 7 }
  19. sub CMD_2WAYOK     { 8 }
  20. sub CMD_2WAYQUEUED { 9 }
  21.  
  22. sub import
  23. {
  24.  my $pkg = shift;
  25.  my $callpkg = caller;
  26.  my @export = ();
  27.  my %export;
  28.  my $export;
  29.  
  30.  @export{@_} = (1) x @_;
  31.  
  32.  foreach $export (@EXPORT)
  33.   {
  34.    if(exists $export{$export})
  35.     {
  36.      push(@export,$export);
  37.      delete $export{$export};
  38.     }
  39.   }
  40.  
  41.  Exporter::export 'Net::SNPP', $callpkg, @export
  42.     if(@_ == 0 || @export);
  43.  
  44.  @export = keys %export;
  45.  Exporter::export 'Net::Cmd',  $callpkg, @export
  46.     if(@_ == 0 || @export);
  47. }
  48.  
  49. sub new
  50. {
  51.  my $self = shift;
  52.  my $type = ref($self) || $self;
  53.  my $host = shift if @_ % 2;
  54.  my %arg  = @_; 
  55.  my $hosts = defined $host ? [ $host ] : $NetConfig{snpp_hosts};
  56.  my $obj;
  57.  
  58.  my $h;
  59.  foreach $host (@{$hosts})
  60.   {
  61.    $obj = $type->SUPER::new(PeerAddr => ($host = $h), 
  62.                 PeerPort => $arg{Port} || 'snpp(444)',
  63.                 Proto    => 'tcp',
  64.                 Timeout  => defined $arg{Timeout}
  65.                         ? $arg{Timeout}
  66.                         : 120
  67.                 ) and last;
  68.   }
  69.  
  70.  return undef
  71.     unless defined $obj;
  72.  
  73.  ${*$obj}{'net_snpp_host'} = $host;
  74.  
  75.  $obj->autoflush(1);
  76.  
  77.  $obj->debug(exists $arg{Debug} ? $arg{Debug} : undef);
  78.  
  79.  unless ($obj->response() == CMD_OK)
  80.   {
  81.    $obj->SUPER::close();
  82.    return undef;
  83.   }
  84.  
  85.  $obj;
  86. }
  87.  
  88.  
  89. sub pager_id
  90. {
  91.  @_ == 2 or croak 'usage: $snpp->pager_id( PAGER_ID )';
  92.  shift->_PAGE(@_);
  93. }
  94.  
  95. sub content
  96. {
  97.  @_ == 2 or croak 'usage: $snpp->content( MESSAGE )';
  98.  shift->_MESS(@_);
  99. }
  100.  
  101. sub send
  102. {
  103.  my $me = shift;
  104.  
  105.  if(@_)
  106.   {
  107.    my %arg = @_;
  108.  
  109.    $me->_PAGE($arg{Pager}) || return 0
  110.     if(exists $arg{Pager});
  111.  
  112.    $me->_MESS($arg{Message}) || return 0
  113.     if(exists $arg{Message});
  114.  
  115.    $me->hold($arg{Hold}) || return 0
  116.     if(exists $arg{Hold});
  117.  
  118.    $me->hold($arg{HoldLocal},1) || return 0
  119.     if(exists $arg{HoldLocal});
  120.  
  121.    $me->_COVE($arg{Coverage}) || return 0
  122.     if(exists $arg{Coverage});
  123.  
  124.    $me->_ALER($arg{Alert} ? 1 : 0) || return 0
  125.     if(exists $arg{Alert});
  126.  
  127.    $me->service_level($arg{ServiceLevel}) || return 0
  128.     if(exists $arg{ServiceLevel});
  129.   }
  130.  
  131.  $me->_SEND();
  132. }
  133.  
  134. sub data
  135. {
  136.  my $me = shift;
  137.  
  138.  my $ok = $me->_DATA() && $me->datasend(@_);
  139.  
  140.  return $ok
  141.     unless($ok && @_);
  142.  
  143.  $me->dataend;
  144. }
  145.  
  146. sub login
  147. {
  148.  @_ == 2 || @_ == 3 or croak 'usage: $snpp->login( USER [, PASSWORD ])';
  149.  shift->_LOGI(@_);
  150. }
  151.  
  152. sub help
  153. {
  154.  @_ == 1 or croak 'usage: $snpp->help()';
  155.  my $me = shift;
  156.  
  157.  return $me->_HELP() ? $me->message
  158.              : undef;
  159. }
  160.  
  161. sub service_level
  162. {
  163.  @_ == 2 or croak 'usage: $snpp->service_level( LEVEL )';
  164.  my $me = shift;
  165.  my $level = int(shift);
  166.  
  167.  if($level < 0 || $level > 11)
  168.   {
  169.    $me->set_status(550,"Invalid Service Level");
  170.    return 0;
  171.   }
  172.  
  173.  $me->_LEVE($level);
  174. }
  175.  
  176. sub alert
  177. {
  178.  @_ == 1 || @_ == 2 or croak 'usage: $snpp->alert( VALUE )';
  179.  my $me = shift;
  180.  my $value  = (@_ == 1 || shift) ? 1 : 0;
  181.  
  182.  $me->_ALER($value);
  183. }
  184.  
  185. sub coverage
  186. {
  187.  @_ == 1 or croak 'usage: $snpp->coverage( AREA )';
  188.  shift->_COVE(@_);
  189. }
  190.  
  191. sub hold
  192. {
  193.  @_ == 2 || @_ == 3 or croak 'usage: $snpp->hold( TIME [, LOCAL ] )';
  194.  my $me = shift;
  195.  my $time = shift;
  196.  my $local = (shift) ? "" : " +0000";
  197.  
  198.  my @g = reverse((gmtime($time))[0..5]);
  199.  $g[1] += 1;
  200.  $g[0] %= 100;
  201.  
  202.  $me->_HOLD( sprintf("%02d%02d%02d%02d%02d%02d%s",@g,$local));
  203. }
  204.  
  205. sub caller_id
  206. {
  207.  @_ == 2 or croak 'usage: $snpp->caller_id( CALLER_ID )';
  208.  shift->_CALL(@_);
  209. }
  210.  
  211. sub subject
  212. {
  213.  @_ == 2 or croak 'usage: $snpp->subject( SUBJECT )';
  214.  shift->_SUBJ(@_);
  215. }
  216.  
  217. sub two_way
  218. {
  219.  @_ == 1 or croak 'usage: $snpp->two_way()';
  220.  shift->_2WAY();
  221. }
  222.  
  223. sub close
  224. {
  225.  my $me = shift;
  226.  
  227.  return 1
  228.    unless (ref($me) && defined fileno($me));
  229.  
  230.  $me->_QUIT && $me->SUPER::close;
  231. }
  232.  
  233. sub DESTROY { shift->close }
  234. sub quit    { shift->close }
  235.  
  236.  
  237. sub debug_text
  238. {
  239.  $_[2] =~ s/^((logi|page)\s+\S+\s+)\S*/$1 xxxx/io;
  240. }
  241.  
  242.  
  243.  
  244. sub _PAGE { shift->command("PAGE", @_)->response()  == CMD_OK }   
  245. sub _MESS { shift->command("MESS", @_)->response()  == CMD_OK }   
  246. sub _RESE { shift->command("RESE")->response()  == CMD_OK }   
  247. sub _SEND { shift->command("SEND")->response()  == CMD_OK }   
  248. sub _QUIT { shift->command("QUIT")->response()  == CMD_OK }   
  249. sub _HELP { shift->command("HELP")->response()  == CMD_OK }   
  250. sub _DATA { shift->command("DATA")->response()  == CMD_MORE }   
  251.  
  252.  
  253. sub _LOGI { shift->command("LOGI", @_)->response()  == CMD_OK }   
  254. sub _LEVE { shift->command("LEVE", @_)->response()  == CMD_OK }   
  255. sub _ALER { shift->command("ALER", @_)->response()  == CMD_OK }   
  256. sub _COVE { shift->command("COVE", @_)->response()  == CMD_OK }   
  257. sub _HOLD { shift->command("HOLD", @_)->response()  == CMD_OK }   
  258. sub _CALL { shift->command("CALL", @_)->response()  == CMD_OK }   
  259. sub _SUBJ { shift->command("SUBJ", @_)->response()  == CMD_OK }   
  260.  
  261.  
  262. 1;
  263. __END__
  264.  
  265. =head1 NAME
  266.  
  267. Net::SNPP - Simple Network Pager Protocol Client
  268.  
  269. =head1 SYNOPSIS
  270.  
  271.     use Net::SNPP;
  272.     
  273.     $snpp = Net::SNPP->new('snpphost');
  274.     $snpp = Net::SNPP->new('snpphost', Timeout => 60);
  275.  
  276. =head1 NOTE
  277.  
  278. This module is not complete, yet !
  279.  
  280. =head1 DESCRIPTION
  281.  
  282. This module implements a client interface to the SNPP protocol, enabling
  283. a perl5 application to talk to SNPP servers. This documentation assumes
  284. that you are familiar with the SNPP protocol described in RFC1861.
  285.  
  286. A new Net::SNPP object must be created with the I<new> method. Once
  287. this has been done, all SNPP commands are accessed through this object.
  288.  
  289. =head1 EXAMPLES
  290.  
  291. This example will send a pager message in one hour saying "Your lunch is ready"
  292.  
  293.     
  294.     use Net::SNPP;
  295.     
  296.     $snpp = Net::SNPP->new('snpphost');
  297.     
  298.     $snpp->send( Pager   => $some_pager_number,
  299.              Message => "Your lunch is ready",
  300.              Alert   => 1,
  301.              Hold    => time + 3600, # lunch ready in 1 hour :-)
  302.            ) || die $snpp->message;
  303.     
  304.     $snpp->quit;
  305.  
  306. =head1 CONSTRUCTOR
  307.  
  308. =over 4
  309.  
  310. =item new ( [ HOST, ] [ OPTIONS ] )
  311.  
  312. This is the constructor for a new Net::SNPP object. C<HOST> is the
  313. name of the remote host to which a SNPP connection is required.
  314.  
  315. If C<HOST> is not given, then the C<SNPP_Host> specified in C<Net::Config>
  316. will be used.
  317.  
  318. C<OPTIONS> are passed in a hash like fashion, using key and value pairs.
  319. Possible options are:
  320.  
  321. B<Timeout> - Maximum time, in seconds, to wait for a response from the
  322. SNPP server (default: 120)
  323.  
  324. B<Debug> - Enable debugging information
  325.  
  326.  
  327. Example:
  328.  
  329.  
  330.     $snpp = Net::SNPP->new('snpphost',
  331.                Debug => 1,
  332.               );
  333.  
  334. =head1 METHODS
  335.  
  336. Unless otherwise stated all methods return either a I<true> or I<false>
  337. value, with I<true> meaning that the operation was a success. When a method
  338. states that it returns a value, failure will be returned as I<undef> or an
  339. empty list.
  340.  
  341. =over 4
  342.  
  343. =item reset ()
  344.  
  345. =item help ()
  346.  
  347. Request help text from the server. Returns the text or undef upon failure
  348.  
  349. =item quit ()
  350.  
  351. Send the QUIT command to the remote SNPP server and close the socket connection.
  352.  
  353. =back
  354.  
  355. =head1 EXPORTS
  356.  
  357. C<Net::SNPP> exports all that C<Net::CMD> exports, plus three more subroutines
  358. that can bu used to compare against the result of C<status>. These are :-
  359. C<CMD_2WAYERROR>, C<CMD_2WAYOK>, and C<CMD_2WAYQUEUED>.
  360.  
  361. =head1 SEE ALSO
  362.  
  363. L<Net::Cmd>
  364. RFC1861
  365.  
  366. =head1 AUTHOR
  367.  
  368. Graham Barr <gbarr@ti.com>
  369.  
  370. =head1 COPYRIGHT
  371.  
  372. Copyright (c) 1995-1997 Graham Barr. All rights reserved.
  373. This program is free software; you can redistribute it and/or modify
  374. it under the same terms as Perl itself.
  375.  
  376. =cut
  377.